
//
//  HMStatusPhotosView.m
//  黑马微博-wzy
//
//  Created by Mac on 2017/2/14.
//  Copyright © 2017年 Mac. All rights reserved.
//

#import "HMStatusPhotosView.h"
#import "HMStatusPhotoView.h"
#import "HMPhoto.h"

#import "MJPhotoBrowser.h"
#import "MJPhoto.h"

//新浪规定一次最多上传9张图片
#define HMStatusMaxPhotoCount 9
//一行最多列数:当图片总数是4的时候,是田字格布局,最多两行;其他最多3行
#define HMStatusMaxClos(photosCount) (photosCount == 4) ? 2:3
//单张图片宽高
#define HMStatusPhotoW 70.0
#define HMStatusPhotoH HMStatusPhotoW
//图片间距
#define HMStatusPhotoMargin 10

@interface HMStatusPhotosView()

@property (nonatomic, strong) UIImageView *imageView;

@property (nonatomic, assign) CGRect lastFrame;

@end


@implementation HMStatusPhotosView


- (instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {

        self.userInteractionEnabled = YES;
        
        //新浪规定一次最多上传9张图片,申请空间是需要消耗内存的,所以一次性创建9张图片,根据具体情况,多余的隐藏
        //尽量不要频繁的创建或销毁
        for (int i = 0; i < HMStatusMaxPhotoCount; i++) {
            HMStatusPhotoView *photoView = [[HMStatusPhotoView alloc] init];
//            photoView.backgroundColor = [UIColor blueColor];
            photoView.tag = i ;
            [self addSubview:photoView];
            
            //给图片添加手势:点击放大
            UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tapPhoto:)];
            [photoView addGestureRecognizer:tap];
        }
        
    }
    return self;
}

//点击图片:添加遮盖(黑色)->将被点击的图片做一个副本添加到遮盖上(与原图相同位置:不同视图间的坐标系转化)->将副本按比例放大
//多张图片的浏览,详见"MJPhotoBrowser.h"/"MJPhoto.h"
- (void)tapPhoto:(UITapGestureRecognizer *)recognizer{
    
//    HMLog(@"点击图片 tag: %ld", recognizer.view.tag);
    
//    //1.添加遮盖
//    UIView *cover = [[UIView alloc] init];
//    cover.backgroundColor = [UIColor blackColor];
//    cover.frame = [UIScreen mainScreen].bounds;
//    //在遮盖上添加手势
//    UITapGestureRecognizer *coverTap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tapCover:)];
//    [cover addGestureRecognizer:coverTap];
//    //将遮盖添加到 主Window上
//    [[UIApplication sharedApplication].keyWindow addSubview:cover];
//    
//    //2.制作图片副本
//    HMStatusPhotoView *photoView = (HMStatusPhotoView *)recognizer.view;
//    
//    UIImageView *tapView = [[UIImageView alloc] init];
////    tapView.image = photoView.image;
//    //加载大图,缩略图只作为占位图
//    [tapView sd_setImageWithURL:[NSURL URLWithString:photoView.photo.bmiddle_pic] placeholderImage:photoView.image];
//    //方法一:将原图的坐标转化到cover
////    tapView.frame = [self convertRect:tapImageView.frame toView:cover];
//    //方法二:cover 的副本坐标是由 原图坐标转化而来
//    tapView.frame = [cover convertRect:photoView.frame fromView:self];
//    //把最初的副本frame保存下来,以便于后期的还原
//    self.lastFrame = tapView.frame;
//    [cover addSubview:tapView];
//    self.imageView = tapView;
//    
//    //3.放大
//    [UIView animateWithDuration:0.25 animations:^{
//        //
//        CGRect frame = tapView.frame;
//        frame.size.width = cover.width;//占据整个屏幕
//        frame.size.height = frame.size.width * (tapView.frame.size.height / tapView.frame.size.width);
//        frame.origin.x = 0;
//        frame.origin.y = (cover.height - frame.size.height) * 0.5;
//        tapView.frame = frame;
//    }];

    
    //****************************************
    //1.创建图片浏览器
    MJPhotoBrowser *browser = [[MJPhotoBrowser alloc] init];
    //2.设置图片浏览器显示的所有图片
    NSMutableArray *photos = [NSMutableArray array];
    
    int count = self.pic_urls.count;
    for (int i = 0;  i < count; i++) {
        HMPhoto *pic = self.pic_urls[i];
        
        MJPhoto *photo = [[MJPhoto alloc] init];
        //设置图片路径
        photo.url = [NSURL URLWithString:pic.bmiddle_pic];
        //设置图片来源于哪一个UIImageView
        photo.srcImageView = self.subviews[i];
        [photos addObject:photo];
    }
    browser.photos = photos;
    
    //3.设置默认显示的图片索引
    browser.currentPhotoIndex = recognizer.view.tag;
    
    //4.显示浏览器
    [browser show];
}


//点击遮盖
- (void)tapCover:(UITapGestureRecognizer *)recognizer{
//    HMLog(@"点击遮盖");
    [UIView animateWithDuration:0.2 animations:^{
        //
        recognizer.view.backgroundColor = [UIColor clearColor];
        //放大的副本,还原为最初的副本frame
        self.imageView.frame = self.lastFrame;
        
    } completion:^(BOOL finished) {
        //移除
        [recognizer.view removeFromSuperview];
        self.imageView = nil;
    }];
}


+ (CGSize)sizeWithPhotosCount:(int)photosCount{

    //最大列数
    int maxCols = HMStatusMaxClos(photosCount);
    
    //总列数
    //判断 photosCount 是否大于maxColumns:如果大于,则总列数为maxColumns;否则,总列数为photosCount
    int totalColumns = photosCount > maxCols ? maxCols:photosCount;
    
    //总行数
    int totalRows;
    //方法一
//    if (photosCount % maxColumns == 0) {
//        totalRows = photosCount / maxColumns;
//    }else{
//        totalRows = photosCount / maxColumns + 1;
//    }
    //方法二:公式
    totalRows = (photosCount + maxCols - 1) / maxCols;
    
    //图片整体view 的宽 高
    CGFloat photosW = totalColumns * HMStatusPhotoW + (totalColumns - 1) * HMStatusPhotoMargin;
    CGFloat photosH = totalRows * HMStatusPhotoH + (totalRows - 1) * HMStatusPhotoMargin;

    return  CGSizeMake(photosW, photosH);
}

//根据传进来的数组,判断有几张图片,显示对应数目的图片
//重写 数组的setter 方法
- (void)setPic_urls:(NSArray *)pic_urls{
    _pic_urls = pic_urls;
    
    
    //遍历9张图片,比较传进来的图片数量与9的大小,
    for (int i = 0; i < HMStatusMaxPhotoCount; i++) {
        
        HMStatusPhotoView *photoView = [[HMStatusPhotoView alloc] init];
        photoView = self.subviews[i];
        if (i < pic_urls.count) {
            //图片显示,传数据
            photoView.photo = pic_urls[i];
            photoView.hidden = NO;
        }else{
            photoView.hidden = YES;
        }
    }

}

- (void)layoutSubviews{
    [super layoutSubviews];
    
    //设置frame
    int count = (int)self.pic_urls.count;
    
    //最大列数
    int maxCols = HMStatusMaxClos(count);
    
    
    for (int i = 0; i < count; i ++) {
        HMStatusPhotoView *photoView = self.subviews[i];
        photoView.width = HMStatusPhotoW;
        photoView.height = HMStatusPhotoH;
        photoView.x = (i % maxCols) * (HMStatusPhotoW + HMStatusCellMargin);
        photoView.y = (i / maxCols) * (HMStatusPhotoH + HMStatusCellMargin);
    }

}



@end
